package controller.validator;

import java.util.ArrayList;
import java.util.Date;

import entity.Course;
import exception.InputValidationErrors;

/**
 * Implements validation for adding, updating or deleting course.
 * 
 * @author huangjm.2012, jaslin.wee.2012
 * 
 */
public class CourseValidator extends Validator {

	private String courseCode;
	private String title;
	private String description;
	private String school;
	private String examDateStr;
	private String examStartTimeStr;
	private String examEndTimeStr;

	private Course course;

	public Course getCourse() {
		return course;
	}

	public CourseValidator(String courseCode, String title, String description,
			String school, String examDate, String examStartTime,
			String examEndTime) {
		super();
		this.courseCode = courseCode;
		this.title = title;
		this.description = description;
		this.school = school;
		this.examDateStr = examDate;
		this.examStartTimeStr = examStartTime;
		this.examEndTimeStr = examEndTime;
	}

	@Override
	public void performValidationBeforeAdding() throws InputValidationErrors {

		/*
		 * Array list to store logic error messages
		 */
		ArrayList<String> logicErrors = new ArrayList<String>();

		/*
		 * Validation for invalid exam date
		 */
		Date examDate = null;
		try {
			examDate = checkForInputDateFormat(examDateStr);
		} catch (Exception e) {
			logicErrors.add(Validator.INVALID_EXAM_DATE);
		}

		/*
		 * Validation for invalid exam date
		 */
		Date examStartTime = null;
		try {
			examStartTime = checkForInputTimeFormat(examStartTimeStr);
		} catch (Exception e) {
			logicErrors.add(Validator.INVALID_EXAM_START);
		}

		/*
		 * Validation for invalid exam date
		 */
		Date examEndTime = null;
		try {
			examEndTime = checkForInputTimeFormat(examEndTimeStr);
		} catch (Exception e) {
			logicErrors.add(Validator.INVALID_EXAM_END);
		}

		/*
		 * Throw exception or create entity
		 */
		if (!logicErrors.isEmpty()) {
			throw new InputValidationErrors(logicErrors);
		} else {
			course = new Course(courseCode, title, description, school,
					examDate, examStartTime, examEndTime);
		}

	}

	@Override
	public void performValidationBeforeUpdating() throws InputValidationErrors {
		// TODO Auto-generated method stub

	}

	@Override
	public void performValidationBeforeDeleting() throws InputValidationErrors {
		// TODO Auto-generated method stub

	}

}
